package com.cntcdt.api.db.dao.base;

import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.ResultSet;
import com.coola.jutil.sql.DBEngine;
import com.coola.jutil.sql.DBFactory;
import com.coola.jutil.data.DataPage;
import com.coola.jutil.sql.ResultPage;
import javax.sql.rowset.CachedRowSet;
import com.cntcdt.api.db.model.AddLineUserDeviceModel;

/**
 * 这是add_line_user_device的Dao实现类的基类
 */
public abstract class AddLineUserDeviceDaoBase {

	/**
	 * 日志处理类实例
	 */
	protected static Log logger = LogFactory.getLog(AddLineUserDeviceDaoBase.class);
	/**
	 * 只读DB引擎
	 */
	protected DBEngine readDBEngine = DBFactory.getDBEngine("main-statistic-b");
	/**
	 * 只写DB引擎
	 */
	protected DBEngine writeDBEngine = DBFactory.getDBEngine("main-statistic");
	/**
	 * add_line_user_device表
	 */
	protected static final String TABLE = "add_line_user_device";

	/**
	 * 默认构造方法
	 */
	public AddLineUserDeviceDaoBase() {
	}

	/**
	 * 插入一条数据
	 * 
	 * @param model
	 *            AddLineUserDeviceModel对象
	 * @return 返回插入影响行数
	 */
	public int insert(AddLineUserDeviceModel model) {
		DBEngine localEngine = DBFactory.getKeepConnDBEngine("main-statistic");
		String sql = "INSERT INTO " + TABLE
				+ " (AddLineUserId,Terminal,AddTime,UpdateTime,opStatus,type,adminUserGroupId) VALUES (?,?,?,?,?,?,?)";
		Object[] paramObj = { model.getAddLineUserId(), model.getTerminal(), model.getAddTime(), model.getUpdateTime(),
				model.getOpStatus(), model.getType(), model.getAdminUserGroupId() };
		int id = 0;
		try {
			id = localEngine.executeUpdate(sql, paramObj);
			ResultSet rs = localEngine.executeQuery("select last_insert_id() as id");
			if (rs.next()) {
				id = rs.getInt("id");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			localEngine.close();
		}
		return id;
	}

	/**
	 * 修改一条数据
	 * 
	 * @param model
	 *            AddLineUserDeviceModel对象
	 * @return 修改成功返回true,否则返回false
	 */
	public boolean update(AddLineUserDeviceModel model) {
		String sql = "UPDATE  " + TABLE
				+ " SET AddLineUserId = ? ,Terminal = ? ,AddTime = ? ,UpdateTime = ? ,opStatus = ? ,type = ? ,adminUserGroupId = ? WHERE Id = ? ";
		Object[] paramObj = { model.getAddLineUserId(), model.getTerminal(), model.getAddTime(), model.getUpdateTime(),
				model.getOpStatus(), model.getType(), model.getAdminUserGroupId(), model.getId() };
		try {
			return writeDBEngine.executeUpdate(sql, paramObj) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 按主键删除一条数据
	 * 
	 * @param id
	 *            id
	 * @return 删除成功返回true,否则返回false
	 */
	public boolean deleteByPrimaryKey(Integer id) {
		String sql = "DELETE FROM " + TABLE + " WHERE id = ?";
		Object[] paramObjs = { id };
		try {
			return writeDBEngine.executeUpdate(sql, paramObjs) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获得model集合
	 * 
	 * @param sql
	 *            查询语句
	 * @return 返回Model集合
	 */
	protected List<AddLineUserDeviceModel> queryModelList(String sql) {
		List<AddLineUserDeviceModel> modelList = new ArrayList<AddLineUserDeviceModel>();
		try {
			ResultSet rs = readDBEngine.executeQuery(sql);
			while (rs.next()) {
				modelList.add(new AddLineUserDeviceModel(rs));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelList;
	}

	/**
	 * 获得model集合
	 * 
	 * @param sql
	 *            查询语句
	 * @param objs
	 *            参数集
	 * @return 返回Model集合
	 */
	protected List<AddLineUserDeviceModel> queryModelList(String sql, Object[] paramObjs) {
		List<AddLineUserDeviceModel> modelList = new ArrayList<AddLineUserDeviceModel>();
		try {
			ResultSet rs = readDBEngine.executeQuery(sql, paramObjs);
			while (rs.next()) {
				modelList.add(new AddLineUserDeviceModel(rs));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelList;
	}

	/**
	 * 按主键查询model
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public AddLineUserDeviceModel findByPrimaryKey(Integer id) {
		String sql = "SELECT * FROM " + TABLE + " WHERE id = ?";
		Object[] paramObjs = { id };
		List<AddLineUserDeviceModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList != null && modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 获得model集合
	 * 
	 * @return 返回Model集合
	 */
	public List<AddLineUserDeviceModel> findAll() {
		String sql = "SELECT * FROM " + TABLE;
		return queryModelList(sql);
	}

	/**
	 * 获分页
	 * 
	 * @param sql
	 *            查询条件
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码
	 * @return 返回分页对象
	 */
	protected DataPage<AddLineUserDeviceModel> findPage(String sql, int pageSize, int pageNo) {
		CachedRowSet rs = null;
		DataPage<AddLineUserDeviceModel> resultList = null;
		try {
			ResultPage page = readDBEngine.queryPage(sql, pageSize, pageNo);
			rs = page.getRecord();
			List<AddLineUserDeviceModel> list = new ArrayList<AddLineUserDeviceModel>();
			while (rs.next()) {
				list.add(new AddLineUserDeviceModel(rs));
			}
			if (list.size() > 0) {
				resultList = new DataPage<AddLineUserDeviceModel>(list, page.getTotalRecordCount(), pageSize, pageNo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

	/**
	 * 获分页
	 * 
	 * @param pageSize
	 *            分页大小
	 * @param pageNo
	 *            页码
	 * @return 返回分页对象
	 */
	public DataPage<AddLineUserDeviceModel> findPage(int pageSize, int pageNo) {
		String sql = "SELECT * FROM " + TABLE;
		return findPage(sql, pageSize, pageNo);
	}
}
